// HeaderEx.cpp : implementation file
//

#include "stdafx.h"
#include "HeaderEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHeaderEx

CHeaderEx::CHeaderEx()
{
	m_nSortCol = -1;
	m_bSortAsc = TRUE;
	
	HBITMAP hBmp = (HBITMAP)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_UDARROW), 
		IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADMAP3DCOLORS|LR_SHARED);

	m_hIml = ImageList_Create(14, 14, ILC_COLOR32|ILC_MASK, 2, 0);
	ImageList_AddMasked(m_hIml, hBmp, GetSysColor(COLOR_BTNFACE));
}

CHeaderEx::~CHeaderEx()
{
	ImageList_Destroy(m_hIml);
}


BEGIN_MESSAGE_MAP(CHeaderEx, CHeaderCtrl)
	//{{AFX_MSG_MAP(CHeaderEx)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
	ON_NOTIFY_REFLECT(NM_RCLICK, OnItemRClick)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHeaderEx message handlers

void CHeaderEx::OnCustomDraw(NMHDR* pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
}

void CHeaderEx::OnItemRClick(NMHDR* pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
}

int CHeaderEx::GetSortCol()
{
	return m_nSortCol;
}


BOOL CHeaderEx::GetSortAsc()
{
	return m_bSortAsc;
}

void CHeaderEx::SetSortStatus(int nSortCol, BOOL bSortAsc, BOOL bRedrawNow)
{
	HDITEM hdItem;
	Header_SetImageList(m_hWnd, m_hIml);
	hdItem.mask = HDI_FORMAT;
	
	if(m_nSortCol != nSortCol)
	{
		Header_GetItem(m_hWnd, m_nSortCol, &hdItem);
		hdItem.fmt &= ~(HDF_IMAGE|HDF_BITMAP_ON_RIGHT);
		Header_SetItem(m_hWnd, m_nSortCol, &hdItem);
	}
	m_nSortCol = nSortCol;
	m_bSortAsc = bSortAsc;
	if(m_nSortCol == -1) return;

	Header_GetItem(m_hWnd, nSortCol, &hdItem);
	hdItem.mask |= HDI_IMAGE;
	hdItem.fmt |= HDF_IMAGE|HDF_BITMAP_ON_RIGHT;
	hdItem.iImage = bSortAsc? 0: 1;

	Header_SetItem(m_hWnd, nSortCol, &hdItem);


	if(bRedrawNow)Invalidate();
}

void CHeaderEx::SetColType(int nCol, ColumnTypeEnum eColType)
{
	m_arrColType.SetAt(nCol, eColType);
}
